package com.xxx.proj.controller;

import com.xxx.proj.entity.Article;
import com.xxx.proj.entity.PageBean;
import com.xxx.proj.entity.Result;
import com.xxx.proj.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
   @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
         PageBean<Article> pb= articleService.list(pageNum,pageSize,categoryId,state);
         return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id) {
       try {
           articleService.delete(id);
       }catch (Exception e){
           return Result.error("删除失败");
       }
       return Result.success();
    }
}
